package cn.fengliu.controller.adm;

import cn.fengliu.Dao.ClzDao;
import cn.fengliu.Dao.CourseDao;
import cn.fengliu.entity.Clz;
import cn.fengliu.entity.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller   //创建对象，并且装配到 IOC容器
@RequestMapping("/course")
public class CourseController {
    @Autowired
    CourseDao courseDao;
    public CourseController() {
        System.out.println("stuController 对象被创建了");
     //当前页数
    }
    @RequestMapping("/show")
    public String findAll(Model model,Integer curpage){

        int total = courseDao.getTotal();//总行数
        System.out.println(total);
        if (curpage==null){
            curpage=1; }//当前页数
        int curpagemax=total/6;//总页数

          if (curpagemax%6!=0){
              curpagemax=curpagemax+1;
          }
        System.out.println(curpagemax);
        List<Course> courses = courseDao.findPage((curpage - 1) * 6, 6);
        System.out.println("show courses"+courses);
          model.addAttribute("curpage" ,curpage);
    model.addAttribute("curpagemax",curpagemax);
//        List<Map<String, Object>> clzs = clzDao.findAll();
        model.addAttribute("courses",courses);


        return "/adm/course/show";
    }
    @RequestMapping("/nextpage")
    public  String nextpage(Integer curpage,Model model){
    if (curpage==null)curpage=1;
        int total = courseDao.getTotal();
        if (curpage==-1){
            curpage=total/6;
        }
       if (curpage*6<total)curpage++;
//        model.addAttribute("Clz",new Clz());
        model.addAttribute("course",new Course());
        return "forward:/course/show?curpage="+curpage;
    }
@RequestMapping("/prevpage")
public  String prevpage(Integer curpage,Model model){
    if (curpage==null)curpage=1;
    if (curpage>1)curpage--;
//    model.addAttribute("Clz",new Clz());
    model.addAttribute("course",new Course());
    return "forward:/course/show?curpage="+curpage;
}


    @GetMapping("/go2add")
    public String go2add(Model model){

   model.addAttribute("action","add");
   Course course=new Course();
   model.addAttribute("course",course);
     return "forward:/course/show";
    }
    @RequestMapping("/add")
    public  String add (Course course, Model model)throws Exception{
        courseDao.add(course);

        return "forward:/course/nextpage?curpage=-1";

    }
    @GetMapping("/go2update")
    public String go2update(Model model ,String cno,Integer curpage){
        if (curpage==null)curpage=1;
        Course course = courseDao.findOne(cno);
        model.addAttribute("action","update");
//        model.addAttribute("clz" ,clz);
        model.addAttribute("cno",course.getCno());
        model.addAttribute("cname",course.getCname());

        return "forward:/course/show?curpage="+curpage;
    }

    @RequestMapping("/update")
    public  String update (Course course, Integer curpage){
        if (curpage==null)curpage=1;
        courseDao.update(course);
        return "forward:/course/show?curpage="+curpage;
    }
    @GetMapping("/del")
     public  String del(Model model,String cno){
        courseDao.del(cno);
        return  "forward:/course/show";
    }

}
